//
//  UTString+To.swift
//  iPhoneTests
//
//  Created by Toj on 3/28/23.
//

import XCTest

class UTString_To: XCTestCase {

    override func setUpWithError() throws {
        //print("-->UTests: start setUpWithError \n")
   
        //print("-->UTests: end setUpWithError \n")
    }

    override func tearDownWithError() throws {
        //print("-->UTests: start tearDownWithError \n")
   
        //print("-->UTests: end tearDownWithError \n")
    }
    
    func testExample() throws {
        print("-->UTests: start \n")
        
        // testFormat()
        
        print("-->UTests: end \n")
    }
    
    func testPerformanceExample() throws {
        print("-->UTests: start testPerformanceExample \n")
        
        self.measure {
            print("-->UTests: start testPerformanceExample measure \n")
         
            print("-->UTests: end testPerformanceExample measure \n")
        }
        
        print("-->UTests: end testPerformanceExample \n")
    }
}

/**
 * 总结:
 * Date 类型是UTC 时间
 * date.to_string UTC -> 本地时间
 * dateString.to_date 本地时间 -> UTC时间
 * 如果服务器返回的是UTC 时间, 必须加上Z的信息 Z == +0000
 * 如果没有返回Z则认为是当前时区的时间, Date 会转换到UTC时间
 * e.g.:
 * "2023-11-09T07:22:13.000+0000"  -> Date
 * +0000代表UTC时间,  date: 2023-11-09 07:22:13.000+0000
 * -0800代表西8区时间, date: 2023-11-09 15:22:13.000+0000
 * +0800代表东8区时间, date: 2023-11-08 23:22:13.000+0000
 */
extension UTString_To {
    
    func testStringToUTC() {
        // "2023-11-09T07:22:13.000+0000"
        let expirationTime = "2023-11-09T07:22:13.000+0000"
        let dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        print("-->UTests: expirationTime: \(expirationTime)")
        
        let df = DateFormatter()
        df.dateFormat = dateFormat
        
        if let expirationDate = df.date(from: expirationTime) {
            // -->UTests: expirationDate: 2023-11-09 07:22:13 +0000
            print("-->UTests: expirationDate: \(expirationDate)")
        }
        
        if let gmtDate = expirationTime.to_gmt(dateFormat) {
            
            // -->UTests: gmtDate: 2023-11-09 07:22:13 +0000
            print("-->UTests: gmtDate: \(gmtDate)")
        }
        
        if let utcDate = expirationTime.to_utc(dateFormat) {
            // -->UTests: utcDate: 2023-11-09 07:22:13 +0000
            print("-->UTests: utcDate: \(utcDate)")
        }
        
        if let date = expirationTime.to_date(dateFormat) {
            // -->UTests: utcDate: 2023-11-09 07:22:13 +0000
            print("-->UTests: date: \(date)")
        }
        
        let dateString = expirationTime.to_format(from: expirationTime, to: "yyyy-MM-dd HH:mm:ss")
        print("-->UTests: dateString: \(dateString)")
    }
    
    func testStringToDate() {
        let calendar = Calendar.current
        
        let df = DateFormatter()
        df.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        let date = Date()
        print("-->UTests: current date: \(date)")
        
        let currentString = df.string(from: date)
        print("-->UTests: current string: \(currentString)")
        
        let string = date.to_string
        print("-->UTests: string: \(string)")
        
        if let newDate = string.to_date("yyyy-MM") {
            print("-->UTests: newDate: \(newDate)")
            
            let startOfDayUTC = calendar.startOfDay(for: newDate)
            print("-->UTests: startOfDayUTC: \(startOfDayUTC)")
            
            let startOfDayLocal = startOfDayUTC.to_local
            print("-->UTests: startOfDayLocal: \(startOfDayLocal)")
        }
        
        let dateToFormatterDate = string.to_format(from: .yyyyMMddHHmmss, to: .yyyyMMddHHmm)
        print("-->UTests: dateToFormatterDate: \(dateToFormatterDate)")
    }
    
    func testStringFormat() {
        
        var dateString = "2023-01-31T23:59:59Z"
        let fromFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
        
        var toFormat   = NSDateFormatType.MMMdyyyy.rawValue
        var result = dateString.to_format(from: fromFormat, to: toFormat)
        print("-->UTests: MMMdyyyy: \(result)")
        
        toFormat   = NSDateFormatType.MMMMdyyyy.rawValue
        result = dateString.to_format(from: fromFormat, to: toFormat)
        print("-->UTests: MMMMdyyyy: \(result)")
        
        toFormat   = NSDateFormatType.MMMMdyyyyHHmmss.rawValue
        result = dateString.to_format(from: fromFormat, to: toFormat)
        print("-->UTests: MMMMdyyyyHHmmss: \(result)")
        
        dateString = "2023-01-31T00:00:01Z"
        let format = NSDateFormatType.yyyyMMddTHHmmssZ
        let tofromat = NSDateFormatType.yyyyMMddHHmmss.replaceSymbol(symbol: .dot)
        
        let dts = dateString.to_format(from: format.to_string, to: tofromat)
        let dts2 = dateString.to_format(from: "", to: "")
        
        print("-->UTests: dts:\(dts)")
        print("-->UTests: dts2:\(dts2)")
        
        let df = DateFormatter()
        df.dateFormat = format.rawValue
        guard let date = df.date(from: dateString) else { return }
        print("-->UTests: date:\(date)")
        df.dateFormat = tofromat
        let str = df.string(from: date)
        print("-->UTests: str:\(str)")
    }
    
    func testDate() {
        let calendar = Calendar.current
        let date = Date()
        print("-->UTests: current date: \(date)")
        
        let string = date.to_string
        print("-->UTests: string: \(string)")
        
        if let newDate = string.to_date() {
            print("-->UTests: newDate: \(newDate)")
            
            let startOfDayUTC = calendar.startOfDay(for: newDate)
            print("-->UTests: startOfDayUTC: \(startOfDayUTC)")
            
            let startOfDayLocal = startOfDayUTC.to_local
            print("-->UTests: startOfDayLocal: \(startOfDayLocal)")
        }
    }
}
